package vip.ola.core.common.util;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * 配置文件内容加密配置<br>
 * <a href='https://github.com/ulisesbocchio/jasypt-spring-boot'>官网</a>
 *
 * @author whitegold
 * @date 2020-06-06 16:13
 */
public class JasyptUtil {

    public final static String SALT = "daify@2023";

    /**
     * 自定义加密器替代默认的加密器
     *
     * @author whitegold
     * @date 2020-06-06 16:13
     */
    public static StringEncryptor createStringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(SALT);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.salt.NoOpIVGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

    public static void main(String args[]){
        StringEncryptor stringEncryptor = JasyptUtil.createStringEncryptor();
        String password = stringEncryptor.encrypt("df001");
        System.out.println(password);
    }
}
